package cn.bdqn.controller;

import cn.bdqn.pojo.SysRight;
import cn.bdqn.service.SysRightService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/right")
public class SysRightController {
    @Resource
    SysRightService sysRightService;
    @RequestMapping("/reload")
    public String reload(){
        sysRightService.reLoadShiro();
        return "redirect:/main";
    }
    /**
     * 列表展示
     * @param rightText
     * @param rightType
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String rightText,String rightType, @RequestParam(defaultValue = "1") Integer pageIndex, Model model){
        //设置每页的显示数据
        Integer pageSize=3;
        //查询每页显示的数据
        IPage<SysRight> rightsByPage = sysRightService.getRightsByPage(rightText, rightType, pageIndex, pageSize);
        model.addAttribute("rightPage",rightsByPage);
        //回显
        model.addAttribute("rightText",rightText);
        model.addAttribute("rightType",rightType);
        return "right/list";
    }
    /**
     * 跳转到新增页面
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model) {
        return "right/add";
    }

    /**
     * 保存方法
     * @param right
     * @return
     */
    @RequestMapping("/save")
    public String save(SysRight right) {
        try {
            sysRightService.addRight(right);
            //跳转
            return "redirect:/right/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到资源新增页面
            return "redirect:/right/add";
        }
    }

    @RequestMapping("/findAllRights")
    @ResponseBody
    public Object findAllRights(String rightType){
        List<SysRight> list = sysRightService.findRightsByRightType(rightType);

        return list;
    }
    /**
     * 跳转到修改页面
     * @param rightCode
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String rightCode, Model model) {
     //根据id查询出要显示的数据表
        SysRight right = sysRightService.getRight(rightCode);
        model.addAttribute("right",right);
        //查询父级资源
        List<SysRight> rightList=null;
        if(right.getRightType().equals("Document")){
            rightList = sysRightService.findRightsByRightType("Folder");
        }else if(right.getRightType().equals("Url")){
            rightList=sysRightService.findRightsByRightType("Document");
        }
        model.addAttribute("rightList",rightList);
        return "right/edit";
    }

    public String update(SysRight right){
        try {
            sysRightService.updateRight(right);
            return "redirect:/right/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到资源修改页面
            return "redirect:/right/edit?rightCode="+right.getRightCode();
        }
    }
    /**
     * 删除操作
     * @param rightCode
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String rightCode) {
        Map<String, Object> map = new HashMap<>();
        try {
            sysRightService.deleteRight(rightCode);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }


}
